<template>
  <div>
    <el-form ref="deptForm" :rules="rules" :model="form" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="form.name"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="form.code"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="form.manager"
          style="width: 80%"
          placeholder="请选择"
        >
          <!-- 渲染函数 -->
          <el-option
            v-for="item in list"
            :key="item.id"
            :value="item.username"
            :label="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="form.introduce"
          style="width: 80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
        <el-button size="small" @click="hCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { getEmployeesSimple } from "@/api/employees";
import {
  addDepartments,
  chaDepartments,
  updeteDepartments,
} from "@/api/departments";
export default {
  props: {
    id: {
      type: String,
      required: true,
    },
    isEdit: {
      type: Boolean,
      required: true,
    },
    originList: {
      type: Array,
      required: true,
    },
  },
  data() {
    // 检验在添加状态时编码在项目是否重复
    const validCode = (rule, value, callback) => {
      let exsitCodeList = this.originList.map((item) => item.code);
      // 编辑状态下 筛选
      if (this.isEdit) {
        exsitCodeList = this.originList
          .filter((item) => item.id !== this.id)
          .map((item) => item.code);
      }
      // 判断是否输入值与传入的值 是否相同
      if (exsitCodeList.includes(value)) {
        callback(new Error(`该部门编码 ${value} 已存在`));
      } else {
        callback();
      }
    };
    // 校验在添加状态的时候 同名的情况
    const validName = (rule, value, callback) => {
      let exisNameList = this.originList
        .filter((item) => item.pid === this.id)
        .map((item) => item.name);
      // 编辑状态下
      if (isEdit) {
        const pid = this.originList.find((item) => item.id === item.id).pid;

        exisNameList = this.originList
          .filter((item) => item.pid === pid)
          .filter((item) => item.id !== this.id)
          .map((item) => item.name);
      }
      if (exisNameList.includes(value)) {
        callback(new Error(`该部门 ${value} 已经存在`));
      } else {
        callback();
      }
    };
    return {
      form: {
        name: "", // 部门名称
        code: "", // 部门编码
        manager: "", // 部门管理者
        introduce: "", // 部门介绍
      },
      list: [], // 储存部门负责人
      rules: {
        name: [
          {
            required: true,
            message: "请输入部门名称",
            trigger: "blur",
          },
          {
            min: 1,
            max: 50,
            message: "请输入1-50个字符",
            trigger: "blur",
          },
          {
            validator: validName,
            trigger: "blur",
          },
        ],
        code: [
          {
            required: true,
            message: "请输入部门编码",
            trigger: "blur",
          },
          {
            min: 1,
            max: 50,
            message: "请输入1-50个字符",
            trigger: "blur",
          },
          {
            validator: validCode,
            trigger: "blur",
          },
        ],
        manager: [
          {
            required: true,
            message: "请输入部门管理者",
            trigger: "blur",
          },
        ],
        introduce: [
          {
            required: true,
            message: "请输入部门名称",
            trigger: "blur",
          },
          {
            min: 1,
            max: 300,
            message: "请输入1-300个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.loadEmployeesSimple();
    // 判断是否渲染详细数据
    if (this.isEdit) {
      this.loadDepartments();
    }
  },
  methods: {
    // 获取部门详细数据
    async loadDepartments() {
      try {
        // 发送请求
        const res = await chaDepartments(this.id);
        console.log(res);
        this.form = res.data;
      } catch (error) {
        console.log(error);
      }
    },
    // 加载部门负责人的信息
    async loadEmployeesSimple() {
      try {
        // 发送请求
        const res = await getEmployeesSimple();
        console.log(res);
        this.list = res.data;
      } catch (error) {
        console.log(error);
      }
    },
    // 添加部门的信息
    async doAdd() {
      try {
        // 发送请求
        const res = await addDepartments({ pid: this.id, ...this.form });
        // console.log(res);
        if (res.code !== 10000) return;
        this.$message.success(res.message);
      } catch (error) {
        console.log(error);
      }
    },
    // 编辑部门的信息
    async doEdit() {
      try {
        // 发送请求
        const res = await updeteDepartments(this.form);
        // console.log(res);
        if (res.code !== 10000) return;
        this.$message.success(res.message);
      } catch (error) {
        console.log(error);
      }
    },
    // 确定
    hSubmit() {
      // 兜底校验
      this.$refs.deptForm.validate((valid) => {
        if (!valid) return; // 校验失败
        // 调用函数 true 编辑  || 添加
        this.isEdit ? this.doEdit() : this.doAdd();
        this.$emit("success");
      });
    },
    // 取消
    hCancel() {
      this.$emit("close");
    },
  },
};
</script>